/**********************
 * 
 * Main.js
 * 
 **********************/

// global variable to store blog posts
var blogposts = new Array();

// global variable to store blog_users
var blogusers = new Array();

// global variable to store the current active user
var currentUser = "51158405";

/*
 * Object constructor to represent a single blog post
 * @method blogpost
 * @param {String}id id of a post
 * @param {String}author author of a post
 * @param {String}title title of a post
 * @param {String}content content of a post
 * @param {String}date updated date of a post
 * @param {String}time updated time of a post
 */
function blogpost(id, author, title, content, date, time) {
	this.id = id;
	this.author = author;
	this.title = title;
	this.content = content;
	this.date = date;
	this.time = time;
}


/**
 * Method to clear the create form
 * @method clearForm

function clearForm() {
	$("#create_user").val("");
	$("#create_pw").val("");
	$("#create_title").val("");
	$("#create_content").val("");
} */

/**
 * Method of handling form validation
 * @method validateForm
 * @return {Boolean}result true if form is validated
 */
function validateForm() {
	var msg = "";
	var result = true;
	
	if( $("#create_user").val() == "" ) {
		msg += "User cannot be empty.\n";
	}
	if( !$("#create_pw").val() ) {
		msg += "Password cannot be empty.\n";
	}
	if( !$("#create_title").val() ) {
		msg += "Title cannot be empty.\n";
	}
	if( !$("#create_content").val() ) {
		msg += "Content cannot be empty.\n";
	}
	if( msg != "" ) {
		alert(msg);
		result = false;
	}
	return result;
}


/**
 * control the scroll to top button
 * source from http://webdesignerwall.com/tutorials/animated-scroll-to-top
 * @method scrollTop
 */
function scrollTop() {
	
	// hide back-top button at first
	$("#back-top").hide();
	
	// fade in #back-top
	$(function () {
		$(window).scroll(function () {
			if ($(this).scrollTop() > 100) {
				$('#back-top').fadeIn();
			} else {
				$('#back-top').fadeOut();
			}
		});

		// scroll body to 0px on click
		$('#back-top a').click(function () {
			$('body,html').animate({
				scrollTop: 0
			}, 800);
			return false;
		});
	});
	
}


/**
 * Method to be triggered when page is loaded
 * @method setup
 */
function setup() {
	getBloggers();
	getPosts(currentUser);
	scrollTop();
	eventHandler(); 
	yui_calendar();
	//yui_autocomplete();
}

$(document).ready(setup);